<template>
	<!-- 添加已录商品 -->
	<view class="wrap-backbg">

		

		<view class="page-box" style="padding-bottom: 80px;">
			<view class="leftpart">
				<u-section class="mt20 mb12 ml16" title="选择商品" :right="false" :show-line="true"></u-section>
				<text class="litem" @click="onTypeChange(-1)">全部</text>
				<text class="litem" v-for="item in typeList" :key="item.id" @click="onTypeChange(item.id)">{{ item.name }}</text>
			</view>
			<view class="rightpart">

				<view class="order" v-for="(res, index) in commodityList" :key="res.id">
					
					<view class="item">
						<view class="left">
							<image :src="res.image" mode="aspectFill"></image>
						</view>
						<view class="content">
							<view class="title u-line-2">{{ res.name }}</view>
							<view class="type">商品id:{{ res.id }}</view>
							<view class="type">商品规格:{{ res.specification }}</view>
							<view class="delivery-time">库存: {{ res.inventory }}{{ res.unit }}</view>
						</view>
						<view class="right">
							<u-checkbox class="exchange" v-model="res.checked" size="28rpx" shape="circle" active-color="#409EFF">
							</u-checkbox>
						</view>

						
					</view>
					
					<view class="bottom">
						

					</view>
				</view>
			</view>

		</view>
		<view class="btnbar">
			<view class="btn" @click="onAddOrder">确认</view>
		</view>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
var _this;
export default {
	data() {
		return {
			/* 卡片 订单列表 */
			commodityList: [],
			RAWcommodityList: [],
			typeList: [],//商品类型列表
		}

	},
	onLoad() {
		_this = this;
		_this.getCommodityList()
		uni.removeStorageSync('commodityList')
		this.getType()
	},
	methods: {
		onTypeChange(id) {
			if (id == -1) {
				this.commodityList = this.RAWcommodityList
			} else {
				console.log('%c [ id ]-113', 'font-size:13px; background:pink; color:#bf2c9f;', id)
				//根据id 过滤相同的商品
				this.commodityList = this.RAWcommodityList.filter(item => item.product_type_id == id)
			}

		},
		getType() {
			this._post_form('api/ykjp/product/type/getType', {}, (res) => {
				console.log('%c [ res ]-113', 'font-size:13px; background:pink; color:#bf2c9f;', res.data.data)
				this.typeList = res.data.data
			})
		},
		getCommodityList() {
			_this._post_form('/api/ykjp/product/product/getProduct', {}, (result) => {
				for (let i = 0; i < result.data.data.length; i++) {
					if (!result.data.data[i].time) {
						result.data.data[i].checked = false
						result.data.data[i].money = '1'
						result.data.data[i].num = '1'
						result.data.data[i].time = _this.transformTime(result.data.data[i].updatetime);
					}
				}
				_this.setData({
					'commodityList': result.data.data
				})
				this.RAWcommodityList = this.commodityList
			});
		},
		onAddOrder() {
			var v = []
			for (let i = 0; i < _this.commodityList.length; i++) {
				if (_this.commodityList[i].checked) {
					v.push(_this.commodityList[i])
				}
			}
			if (v.length < 1) {
				return _this.$refs.uToast.show({
					title: '请选择一个商品',
					type: 'default'
				})
			}
			uni.setStorageSync('commodityList', v);

			uni.navigateBack()
		}
	}
}
</script>


<style scoped lang="scss">
.page-box {
	display: flex;
	gap: 20px;

	.leftpart {
		background: #ffffff;
		width: calc(30% - 20px);
		height: 100%;
		display: flex;
		gap: 10px;
		flex-direction: column;
		position: fixed;
		left: 0px;
		top: 0px;

		.litem {
			padding: 4px;
			text-align: center;
			width: 100%;
			border-bottom: 1px solid rgba(0, 0, 0, 0.1);

		}
	}

	.rightpart {
		position: absolute;
		right: 10px;
		top: 0px;
		width: calc(70% - 10px);
		padding-bottom: 80px;
	}
}

.btnbar {
	position: fixed;
	bottom: 0px;
	left: 0px;
	width: 100%;
	height: 80px;
	background-color: #ffffff;
	border-top: 1px solid rgba(0, 0, 0, 0.1);

	.btn {
		position: absolute;
		right: 20px;
		bottom: 20px;
		height: 40px;
		width: 100px;
		background-color: #409EFF;
		line-height: 40px;
		text-align: center;
		color: #ffffff;
		border-radius: 10px;
	}
}

.title-wrap {
	display: inline-block;
}

.title {
	text-align: left;
	font-size: 28rpx;
	color: $u-content-color;
	margin-top: 20rpx;
}

.wrap-backbg {
	// padding: 30rpx;
	display: flex;
	flex-direction: column;
	height: calc(100vh - var(--window-top));
	width: 100%;
}

.order {
	width: 100%;
	background-color: #ffffff;
	margin: 20rpx auto;
	border-radius: 20rpx;
	box-sizing: border-box;
	padding: 20rpx;
	font-size: 28rpx;

	.top {
		display: flex;
		justify-content: space-between;

		.left {
			display: flex;
			align-items: center;

			.store {
				margin: 0 10rpx;
				font-size: 32rpx;
				font-weight: bold;
			}
		}

		.right {}
	}

	.item {
		display: flex;
		margin: 20rpx 0 0;

		.left {
			margin-right: 20rpx;

			image {
				width: 160rpx;
				height: 160rpx;
				border-radius: 10rpx;
			}
		}

		.content {
			width: 200px;

			.title {
				font-size: 28rpx;
				line-height: 50rpx;
			}

			.type {
				margin: 10rpx 0;
				font-size: 24rpx;
				color: $u-tips-color;
			}

			.delivery-time {
				color: #e5d001;
				font-size: 24rpx;
			}
		}

		.right {
			margin-left: 10rpx;
			padding-top: 20rpx;
			// text-align: right;
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			color: $u-type-warning-dark;

			.decimal {
				font-size: 24rpx;
				margin-top: 4rpx;
			}

			.number {
				color: $u-tips-color;
				font-size: 24rpx;
			}
		}
	}

	.total {
		margin-top: 20rpx;
		text-align: right;
		font-size: 24rpx;

		.total-price {
			font-size: 32rpx;
		}
	}

	.bottom {
		display: flex;
		margin-top: 40rpx;
		padding: 0 10rpx;
		justify-content: space-between;
		align-items: center;

		.btn {
			line-height: 52rpx;
			width: 160rpx;
			border-radius: 26rpx;
			border: 2rpx solid $u-border-color;
			font-size: 26rpx;
			text-align: center;
			color: $u-type-info-dark;
		}

		.evaluate {
			color: $u-type-warning-dark;
			border-color: $u-type-warning-dark;
		}
	}
}
</style>
